package c2.group.business.gradepvp;

import c2.advanced.business.base.AdvancedDaoTemplate;
import c2.advanced.share.DbFacade;
import cate.game.role.pvp.gradepvp.GradePvpPlayer;
import com.mongodb.client.result.DeleteResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Collection;
import java.util.List;

@Slf4j
public class GradePvpDao extends AdvancedDaoTemplate {
    public static final String POOL_COLLECTION = "grade-pvp-pool";

    public GradePvpDao(DbFacade root) {
        super(root);
    }

    public List<GradePvpPlayer> getPoolPlayers() {
        long beginTime = System.currentTimeMillis();
        List<GradePvpPlayer> list = this.mongo().findAll(GradePvpPlayer.class, POOL_COLLECTION);
        log.info("段位赛-玩家池拉取结束 ,耗时{}ms 共有{}个玩家", (System.currentTimeMillis() - beginTime), list.size());
        return list;
    }

    public void savePlayers(Collection<GradePvpPlayer> list) {
        DeleteResult dr = this.mongo().remove(new Query(), POOL_COLLECTION);
        log.info("段位赛-玩家池更新list前先删除数据{}条, wasAcknowledged={}", dr.getDeletedCount(), dr.wasAcknowledged());
        Collection<GradePvpPlayer> r = this.mongo().insert(list, POOL_COLLECTION);
        log.info("段位赛-玩家池试图insert {} 个数据, 实际插入 {} 个数据", list.size(), r.size());
    }
}
